package com.cdl.library.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cdl.library.R;
import com.cdl.library.listener.MoreButtonToggleListener;
import com.cdl.library.util.EventClick;


/***
 * 手机端topBar统一类型
 *
 * @author Administrator
 */
public class MoreButtonToggle extends RelativeLayout {

    private final TextView tv_content;
    private View view_top;
    private OnKeyListener onKeyListener;
    private boolean disable;

    public MoreButtonToggle(Context context) {
        this(context, null);
    }

    public MoreButtonToggle(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    View view;
    String title_content = "";
    int imageId = R.drawable.ic_launcher;
    boolean showright;
    View view_bottom;
    boolean showbottom;
    boolean showtop;
    MyToggleButton toggle_switch;
    //    int defaultTextColor = 0xffffffff;
    int defaultTextColor;
    int disableTextColor = Color.GRAY;
    TextView tv_right_text;
    RelativeLayout lin_toggle_layout;
    private EventClick mClick = new EventClick();

    public MoreButtonToggle(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mClick.setOneDelay(600);
        defaultTextColor = getResources().getColor(R.color.setting_title_color);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.More_Button);
        title_content = a.getString(R.styleable.More_Button_title_content);
        String rightText = a.getString(R.styleable.More_Button_right_content);
        imageId = a.getResourceId(R.styleable.More_Button_image_left, R.drawable.ic_launcher);
        showright = a.getBoolean(R.styleable.More_Button_showright, true);
        defaultTextColor = a.getColor(R.styleable.More_Button_text_font_color, defaultTextColor);
        int rightTextColor = a.getColor(R.styleable.More_Button_text_right_color, defaultTextColor);
        float textSize = a.getDimension(R.styleable.More_Button_text_size, 12);

//        defaultTextColor = a.getInt(R.styleable.More_Button_text_font_color, 0xffffffff);
        showbottom = a.getBoolean(R.styleable.More_Button_showbottom, true);
        showtop = a.getBoolean(R.styleable.More_Button_showtop, true);
        view = LayoutInflater.from(context).inflate(R.layout.more_button_toggle, null);

        lin_toggle_layout = (RelativeLayout) view.findViewById(R.id.lin_toggle_layout);

        tv_content = (TextView) view.findViewById(R.id.tv_content);
        tv_right_text = (TextView) view.findViewById(R.id.tv_right_text);
        tv_content.setText(title_content);
        tv_right_text.setText(rightText);
        tv_content.setTextColor(defaultTextColor);
        tv_right_text.setTextColor(rightTextColor);
        view_top = view.findViewById(R.id.view_top);
        view_bottom = view.findViewById(R.id.view_bottom);

        if (textSize > 0) {
            tv_content.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);
            tv_right_text.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);
        }

        view_top.setVisibility(showtop ? View.VISIBLE : View.INVISIBLE);
        view_bottom.setVisibility(showbottom ? View.VISIBLE : View.INVISIBLE);

        toggle_switch = (MyToggleButton) view.findViewById(R.id.toggle_switch);
        toggle_switch.setIsChoice(false);
        toggle_switch.setOnToggleListener(new MyToggleButton.ToggleClickListener() {
            @Override
            public void click(View view, boolean isClick) {
                if (disable) {
                    return;
                }
                reciveChekcStatues = isClick;
                if (listener != null) {
                    listener.switchToggleView(MoreButtonToggle.this, isClick);
                }
            }
        });

        lin_toggle_layout.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (disable || !mClick.isOneClick()) {
                    return;
                }
                reciveChekcStatues = !reciveChekcStatues;
                toggle_switch.setIsChoice(reciveChekcStatues);
                if (listener != null) {
                    listener.switchToggleView(MoreButtonToggle.this, reciveChekcStatues);
                }
            }
        });

        lin_toggle_layout.setOnKeyListener(new OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (onKeyListener != null) {
                    return onKeyListener.onKey(MoreButtonToggle.this, keyCode, event);
                }
                return false;
            }
        });


        addView(view);
        a.recycle();
    }

    boolean reciveChekcStatues = false;

    public void setChcekcAble(boolean isCheck) {
        reciveChekcStatues = isCheck;
        toggle_switch.setIsChoice(isCheck);
    }

    public void setKeyListener(OnKeyListener onKeyListener) {
        this.onKeyListener = onKeyListener;
    }

    public void setTitleFontTextColor(int textColor) {
        tv_content.setTextColor(textColor);
    }

    public void setSwitchViewDisable(boolean disable) {
        this.disable = disable;
//        toggle_switch.setDisable(disable);
        tv_content.setTextColor(disable ? disableTextColor : defaultTextColor);
    }

    public boolean getCheckStatues() {
        return reciveChekcStatues;
    }

    public boolean isChecked() {
        return toggle_switch.isChoice;
    }

    public void setContentText(String text) {
        tv_content.setText(text);
    }

    public void setRightContentText(String text) {
        tv_right_text.setText(text);
    }

    public String getContentText() {
        return tv_content.getText().toString();
    }

    public void setClickAroundEnable(boolean enable) {
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        if (changed) {
            layout(l, t, r, b);
        }
    }

    MoreButtonToggleListener listener;

    public void setOnMoretListener(MoreButtonToggleListener listener) {
        this.listener = listener;
    }


}
